package com.wlx.common.message.email;

import com.wlx.common.core.utils.MapUtil;
import com.wlx.common.message.Sender;
import com.wlx.common.message.util.MailUtil;
import com.wlx.common.message.util.scheduler.SchedulerUtil;

import java.util.Map;

/**
 * @author: Admin
 * @date: 2020/6/11 9:14
 * @version: 1.0.0
 */
public abstract class AbstractEmailSender implements Sender {

    @Override
    public boolean send(String receiver, String message, String title, Map<String, Object> smsSenderParameter) throws Exception {
        String host = smsSenderParameter.get("host").toString();
        Integer port = Integer.valueOf(smsSenderParameter.get("port").toString());
        String username = smsSenderParameter.get("username").toString();
        String protocol = smsSenderParameter.get("protocol").toString();
        String password = smsSenderParameter.get("password").toString();
        String from = smsSenderParameter.get("from").toString();
        MailUtil build = MailUtil.build(host, port, protocol, username, password);
        build.send(from, receiver, title, message);
        return true;
    }

    @Override
    public boolean syncSend(String phone, String message, String title, Map<String, Object> smsSenderParameter) throws Exception {
        SchedulerUtil.schedule(() -> {
            send(phone, message, title, smsSenderParameter);
        });
        return true;
    }

    @Override
    public Map<String, Object> senderParameterTemplate() {
        return MapUtil.builder()
                .elem("protocol", "示例：smtp")
                .elem("port", "示例：465")
                .elem("host", "示例：smtp.163.com")
                .elem("username", "示例：plutusdog@163.com")
                .elem("password", "示例：xxxxxx")
                .elem("from", "示例：plutusdog@163.com")
                .build();
    }
}
